const { Client } = require('pg');
const fs = require('fs');
const path = require('path');

// Database configuration
const dbConfig = {
  host: process.env.DB_HOST || 'localhost',
  port: process.env.DB_PORT || 5432,
  database: process.env.DB_NAME || 'financeapp',
  user: process.env.DB_USER || 'beauty_admin', // Changed from 'postgres' to 'beauty_admin'
  password: process.env.DB_PASSWORD || 'Beauty5alon2025SecureKey', // Use the correct password
};

async function initDatabase() {
  const client = new Client(dbConfig);
  
  try {
    // Connect to database
    await client.connect();
    console.log('Database connected successfully');
    
    // Read SQL file
    const sqlFilePath = path.join(__dirname, 'init-db.sql');
    const sql = fs.readFileSync(sqlFilePath, 'utf8');
    
    // Execute SQL
    await client.query(sql);
    console.log('Database initialized successfully');
  } catch (error) {
    console.error('Error initializing database:', error);
  } finally {
    await client.end();
  }
}

// Run initialization
initDatabase();